package com.sanpu.kxe.business.trade.view;

import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.trade.contract.TradeConfig;
import com.sanpu.kxe.common.utils.CommonUtils;
import com.sanpu.kxe.common.utils.DateCheckListener;
import com.sanpu.kxe.jump.eventbus.FilterEvent;
import com.sanpu.kxe.module.roundview.RoundLinearLayout;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.module.wheel.interfaces.IWheelDataChangeCallback;
import com.sanpu.kxe.utils.DateUtils;
import com.sanpu.kxe.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.Calendar;

import butterknife.Bind;

/**
 * Description：账单日期筛选
 * Created by 薛勇军 on 2018/8/27.
 */

public class TransChooseDateActivity extends BaseTitleActivity {

    @Bind(R.id.chooseRll)
    RoundLinearLayout chooseRll;
    @Bind(R.id.showChooseTv)
    TextView showChooseTv;
    @Bind(R.id.startDateRtv)
    RoundTextView startDateRtv;
    @Bind(R.id.endDateRtv)
    RoundTextView endDateRtv;
    @Bind(R.id.llStartDate)
    LinearLayout llStartDate;
    @Bind(R.id.llEndDate)
    LinearLayout llEndDate;

    @Bind(R.id.dayLayout)
    LinearLayout dayLayout;
    @Bind(R.id.monthLayout)
    LinearLayout monthLayout;
    @Bind(R.id.dateRtv)
    RoundTextView dateRtv;
    @Bind(R.id.llContentMonth)
    LinearLayout llContentMonth;

    private FilterEvent mFilterEvent;
    private Calendar mCalendar = Calendar.getInstance();

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_trans_choose_date;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("选择日期");
        commonTitle.showRightTextView("完成", ContextCompat.getColor(mContext, R.color.color_base));
        initFilter();
        //设置默认时间
        if (TextUtils.isEmpty(startDateRtv.getText().toString())) {
            startDateRtv.setText(CommonUtils.getMonthFirstDay());
        }
        if (TextUtils.isEmpty(endDateRtv.getText().toString())) {
            endDateRtv.setText(DateUtils.formatTime(mCalendar.getTime().getTime(), "yyyy-MM-dd"));
        }
        if (TextUtils.isEmpty(dateRtv.getText().toString())) {
            String dateStr = DateUtils.formatTime(mCalendar.getTime().getTime(), "yyyy-MM");
            dateRtv.setText(dateStr);
        }
        new DateCheckListener(this).handleRegionCheck(llStartDate, startDateRtv, true);
        new DateCheckListener(this).handleRegionCheck(llEndDate, endDateRtv, true);
        new DateCheckListener(this).handleRegionCheck(llContentMonth, dateRtv, false, new IWheelDataChangeCallback() {
            @Override
            public void wheelDataChangeCallback(String wheelData) {
                if (!TextUtils.isEmpty(wheelData) && wheelData.contains("-")) {
                    String[] split = wheelData.split("-");
                    if (split.length >= 3) {
                        dateRtv.setText(split[0]+"-"+split[1]);
                    } else {
                        dateRtv.setText(wheelData);
                    }
                }
            }
        });
    }

    @Override
    protected void setListener() {
        super.setListener();
        commonTitle.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mFilterEvent == null)
                    mFilterEvent = new FilterEvent();
                String showChoose = showChooseTv.getText().toString();
                if (TextUtils.equals("按日选择", showChoose)) {
                    mFilterEvent.setTimeType("按日选择");
                    String startTime = startDateRtv.getText().toString();
                    String endTime = endDateRtv.getText().toString();
                    if (endTime.compareTo(startTime) < 0) {
                        ToastUtil.showTextViewPrompt("筛选开始时间不能大于结束时间");
                        return;
                    }
                    mFilterEvent.setStartTime(startDateRtv.getText().toString());
                    mFilterEvent.setEndTime(endDateRtv.getText().toString());
                    mFilterEvent.setMonth("");
                } else {
                    mFilterEvent.setTimeType("按月选择");
                    mFilterEvent.setStartTime("");
                    mFilterEvent.setEndTime("");
                    mFilterEvent.setMonth(dateRtv.getText().toString());
                }
                EventBus.getDefault().post(mFilterEvent);
                finish();
            }
        });
        chooseRll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String showChoose = showChooseTv.getText().toString();
                if (TextUtils.equals("按日选择", showChoose)) {
                    showChooseTv.setText("按月选择");
                    dayLayout.setVisibility(View.GONE);
                    monthLayout.setVisibility(View.VISIBLE);
                } else {
                    showChooseTv.setText("按日选择");
                    dayLayout.setVisibility(View.VISIBLE);
                    monthLayout.setVisibility(View.GONE);
                }
            }
        });
        startDateRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startDateRtv.getDelegate().setStrokeColor(ContextCompat.getColor(mContext, R.color.c_4a4a4a));
                endDateRtv.getDelegate().setStrokeColor(ContextCompat.getColor(mContext, R.color.c_c4c4c4));
                llStartDate.setVisibility(View.VISIBLE);
                llEndDate.setVisibility(View.GONE);
            }
        });
        endDateRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startDateRtv.getDelegate().setStrokeColor(ContextCompat.getColor(mContext, R.color.c_c4c4c4));
                endDateRtv.getDelegate().setStrokeColor(ContextCompat.getColor(mContext, R.color.c_4a4a4a));
                llStartDate.setVisibility(View.GONE);
                llEndDate.setVisibility(View.VISIBLE);
            }
        });
    }

    /**
     * 填充之前选中的状态
     */
    public void initFilter() {
        mFilterEvent = getIntent().getParcelableExtra(TradeConfig.INTENT_FILTER);
        if (mFilterEvent != null) {
            if (TextUtils.equals("按日选择", mFilterEvent.getTimeType())) {
                showChooseTv.setText("按日选择");
                dayLayout.setVisibility(View.VISIBLE);
                monthLayout.setVisibility(View.GONE);
            } else {
                showChooseTv.setText("按月选择");
                dayLayout.setVisibility(View.GONE);
                monthLayout.setVisibility(View.VISIBLE);
            }
            if (!TextUtils.isEmpty(mFilterEvent.getStartTime())) {
                startDateRtv.setText(mFilterEvent.getStartTime());
            }
            if (!TextUtils.isEmpty(mFilterEvent.getEndTime())) {
                endDateRtv.setText(mFilterEvent.getEndTime());
            }
            if (!TextUtils.isEmpty(mFilterEvent.getMonth())) {
                dateRtv.setText(mFilterEvent.getMonth());
            }
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }
}
